﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;
using LoveMusic.Models;
namespace LoveMusic.Helpers
{
    public class Bars
    {
        public float confidence
        { get; set; }

        public float value
        { get; set; }
    }

    public class Beats
    {
        public float confidence
        { get; set; }

        public float value
        { get; set; }
    }

    public class Sections
    {
        public float start;
        public float duration;
    }

    public class Segments
    {
        
    }

    public class EchoHolder
    {
        public List<Bars> bars
        {
            get;
            set;
        }

        public List<Beats> beats
        {
            get;
            set;
        }

        public float duration
        {
            get;
            set;
        }

        public float get_key
        {
            get;
            set;
        }

        public float confidence_key
        {
            get;
            set;
        }

        public float loudness
        {
            get;
            set;
        }

        public string metadata_artist;
        public string metadata_release;
        public string metadata_title;
        public int metadata_duration;
        public int metadata_samplerate;
        public int metadata_bitrate;

        public int confidence_mode;
        public int mode; // major or minor 0,1

        public List<Sections> sections;


    }
    
    public class ECHONEST
    {
        public string API_KEY
        {
            private get;
            set;
        }

        public void get_mp3(string inputUrl)
        {
            string id = string.Empty;
            List<Bars> listMP3Beats = new List<Bars>();

            #region UploadMp3

            Uri uri = new Uri("http://developer.echonest.com/api/upload?api_key=GZWEIGV8SGDQMITRF&url="+inputUrl+"&version=3");
            string xml = GetResponse(uri.ToString());
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xml));

            foreach (XmlElement element in xmlDoc.GetElementsByTagName("track"))
            {
                id=element.Attributes["id"].Value;
                break;
            }

            #endregion UploadMp3

            if (id != string.Empty)
            {
                uri = new Uri("http://developer.echonest.com/api/get_bars?api_key=GZWEIGV8SGDQMITRF&id="+id+"&version=3");
                xml = GetResponse(uri.ToString());
                xmlDoc = new XmlDocument();
                xmlDoc.Load(new StringReader(xml));
                foreach (XmlElement element in xmlDoc.GetElementsByTagName("bar"))
                {
                    Bars mp3BarsBin = new Bars();

                    mp3BarsBin.confidence = float.Parse(element.Attributes["confidence"].Value);
                    mp3BarsBin.value = float.Parse(element.InnerXml);
                    listMP3Beats.Add(mp3BarsBin);
                }
            }
        }

        /// <summary>
        /// Get the xml 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetResponse(string url)
        {
            try
            {
                WebClient wc = new WebClient();
                //wc.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");

                //that solves the problem partially:
               // wc.Proxy = null;

               // wc.Encoding = System.Text.Encoding.UTF8;
              //  wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                using (Stream stream  = wc.OpenRead(url))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
                //return null;
            }
        }
    }
}